<template>
  <div class="app_content">
    <el-dialog  v-model="open"  :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" width="80%"  append-to-body draggable :fullscreen="fullscreen">
      <template #header="{close,titleId, titleClass }">
      <div class="my-header">
        <span :id="titleId" :class="titleClass">{{title}}</span>
        <div>
          <component
        style="cursor:pointer;line-height: 24px;margin-right:10px;"
        v-if="fullscreen==false"
              @click="fullscreen=true"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-full-screen-one'"
            />
            <component
            v-else
        style="cursor:pointer;line-height: 24px;margin-right:10px;"
              @click="fullscreen=false"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-off-screen-one'"
            />
        <component
        style="cursor:pointer;line-height: 24px;"
              @click="close"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-close'"
            />
        </div>
        
      </div>
    </template>
      <el-form :inline="true" ref="queryForm" :model="queryParams" class="demo-form-inline" :size="size" v-if="showSearch">
        <el-form-item label="用户名称">
      <el-input v-model="queryParams.userName" clearable placeholder="用户名称" style="width: 240px"/>
      </el-form-item>
      <el-form-item label="电话号码">
      <el-input v-model="queryParams.phonenumber" clearable placeholder="电话号码" style="width: 240px"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="querySubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-search'"/>
          搜索</el-button>
        <el-button   @click="resetSubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-redo'"/>
          重置</el-button>
      </el-form-item>
      </el-form>
       
      <el-table @row-click="clickRow" ref="tableRef" v-loading="loading" :data="tableData" style="width: 100%;height:320px" @selection-change="handleSelectionChange"  :size="size"
      >
      <el-table-column type="selection" width="55" />
      <el-table-column label="编号" prop="id" show-overflow-tooltip key="id" >
        <template #default="scope">{{scope.$index+1}}</template>
        </el-table-column>
      <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" key="userName"  />
      <el-table-column label="用户昵称" prop="nickName" :show-overflow-tooltip="true" key="nickName"  />
      <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true" key="email"  />
      <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" key="phonenumber" />
      <el-table-column label="状态" prop="status" :formatter="statusFormat" show-overflow-tooltip key="status">
        </el-table-column>
      <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip key="createTime"/>
      
  </el-table>
  <div style="margin-top:10px;position: absolute;right: 0;margin-right:20px">
      <el-pagination
        v-model:currentPage="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
  </div>
  <template #footer>
        <div  class="dialog-footer" style="margin-top:30px">
          <el-button type="primary" @click="submitForm" :size="size">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-correct'"/>
          确 定</el-button>
        <el-button @click="cancel" :size="size">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-return'"/>
          取 消</el-button>
      </div>
      </template>
  </el-dialog>
  
  </div>
  
</template>

<script  >
import { unallocatedUserList, authUserSelectAll } from "@/api/system/role";
import { computed, ref ,reactive,onMounted,getCurrentInstance,toRefs} from 'vue'
import { useStore } from 'vuex';
import {
Delete,
Edit,
Plus,
Close
} from '@element-plus/icons-vue'

export default defineComponent({
  props: {
    // 角色编号
    roleId: {
      type: [Number, String]
    }
  },
setup(props, { emit }){
  const { proxy } = getCurrentInstance();//关键代码
  const queryForm=ref(null)
  const tableRef=ref(null)
  const store = useStore();
  const staticData=reactive({
    formData:{
    },
    // 选中数组值
    userIds: [],
    // 未授权用户数据
    userList: [],
    //是否全屏
    fullscreen:false,
    // 弹出层标题
    title: "",
      // 是否显示弹出层
    open: false,
    single:true,
    multiple:true,
    showSearch:true,
    background:false,
    disabled :false,
    search:'',
    tableData:[],
    // 日期范围
    dateRange: [],
    // 菜单状态数据字典
    statusOptions: [],
    // 查询参数
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      roleId: undefined,
      userName: undefined,
      phonenumber: undefined
    },
    total:0,
    loading:true, 
    ExportHead:{
      id: "编号",
      name: "角色名称",
      age: "权限字符",
      xb: "显示顺序",
      des: "状态",
      date: "创建时间",
    },
  })
  const size = computed(() => {
        return store.getters['setting/size'];
  });

  const small =computed(() => {
    return size.value=='small'?true:false
  });

  const filterTableData = computed(() =>
    staticData.tableData.filter(
      (data) =>
        !staticData.search.value ||
        data.name.toLowerCase().includes(staticData.search.value.toLowerCase())
    )
  )
    onMounted(() => {
      proxy.$getDicts("sys_normal_disable").then(response => {
        staticData.statusOptions = response.data;
      });
      
    })
  // 显示弹框
  const  show=()=> {
    console.log(props.roleId,'--------------')
      staticData.queryParams.roleId = props.roleId;
      proxy.getList();
      staticData.title='选择用户'
      staticData.open = true;
    }


  const methods = {
    
     getList(){
        staticData.loading = true;
        unallocatedUserList(staticData.queryParams).then(
          response => {
            staticData.tableData = response.rows;
            staticData.total = response.total;
            staticData.loading = false;
          }
      );
     },
     clickRow(row) {
      tableRef.value.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      staticData.userIds = selection.map(item => item.userId);
    },
     
    // 岗位状态字典翻译
    statusFormat(row, column) {
      const data=proxy.$selectDictLabel(staticData.statusOptions, row.status)
      let stateClass
      // if(data=='正常'){
      //   stateClass='status_success'
      // }else{
      //   stateClass='status_error'
      // }
      
      return data;
    },
     /**
      * 显示或隐藏搜索条件
      **/
     showSearchClick(data){
       staticData.showSearch=data
     },
     /** 选择授权用户操作 */
     submitForm() {
      const roleId = staticData.queryParams.roleId;
      const userIds = staticData.userIds.join(",");
      if (userIds == "") {
        proxy.Error("请选择要分配的用户");
        return;
      }
      authUserSelectAll({ roleId: roleId, userIds: userIds }).then(res => {
        proxy.Success(res.msg);
        if (res.code === 200) {
          staticData.open = false;
          proxy.$emit('ok');
        }
      });
    },
    cancel(){
      staticData.open = false;
    },
    resetSubmit(){
      staticData.dateRange=[]
      queryForm.value.clearValidate()
      queryForm.value.resetFields();
      proxy.getList()
    },
    querySubmit(){
      staticData.queryParams.pageNum = 1;
      proxy.getList()
    },
    handleSizeChange (val) {
      console.log(`${val} items per page`)
      staticData.queryParams.pageSize =val;
      proxy.getList()
    },
    handleCurrentChange(val){
      console.log(`current page: ${val}`)
      staticData.queryParams.pageNum =val;
      proxy.getList()
    }
   }
    return {
      Delete,
      Edit,
      Plus,
      Close,
      size,
      small,
      show,
      queryForm,
      tableRef,
      ...methods,
      filterTableData,
      ...toRefs(staticData)
    }
}


})



</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
margin-top: 10px;
}
.demo-pagination-block .demonstration {
margin-bottom: 16px;
}
</style>
<style scoped>
.app_content{
  
  margin-top: 30px;
  margin-left: 10px;
}
.mb8{
  margin-bottom: 10px;
}

</style>
